<template>
  <el-dialog
    :title="!dataForm.storeId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <!-- <el-form-item label="${column.comments}" prop="storeId">
      <el-input v-model="dataForm.storeId" placeholder="${column.comments}"></el-input>
      </el-form-item>-->
      <el-form-item label="商品名称" prop="produceName">
        <el-input
          v-model="dataForm.produceName"
          placeholder="商品名称"
        ></el-input>
      </el-form-item>
      <el-form-item label="所属分类" prop="classifyId">
        <template>
          <el-select v-model="dataForm.classifyId" placeholder="请选择所属分类">
            <el-option
              v-for="item in dataForm.classifies"
              :key="item.value"
              :label="item.text"
              :value="item.value"
            ></el-option>
          </el-select>
        </template>
      </el-form-item>

      <!-- <el-form-item label="所属目录" prop="categoryId">
        <el-input v-model="dataForm.categoryId" placeholder="所属目录"></el-input>
      </el-form-item>-->
      <el-form-item label="所属目录" prop="categoryId">
        <template>
          <el-select v-model="dataForm.categoryId" placeholder="请选择所属目录">
            <el-option
              v-for="item in dataForm.categories"
              :key="item.value"
              :label="item.text"
              :value="item.value"
            ></el-option>
          </el-select>
        </template>
      </el-form-item>

      <el-form-item label="商品价格" prop="producePrice">
        <el-input
          v-model="dataForm.producePrice"
          placeholder="商品价格"
        ></el-input>
      </el-form-item>
      <el-form-item label="商品库存" prop="produceStock">
        <!-- <el-input v-model="dataForm.produceStock" placeholder="商品库存"></el-input> -->
        <el-input-number
          v-model="dataForm.produceStock"
          :min="0"
          label="商品库存"
        ></el-input-number>
      </el-form-item>
      <el-form-item label="商品状态" prop="produceStatus">
        <!-- <el-input v-model="dataForm.produceStatus" placeholder="商品状态"></el-input> -->
        <el-switch
          v-model="dataForm.produceStatus"
          active-color="#13ce66"
          inactive-color="#ff4949"
          :active-value="1"
          :inactive-value="0"
        ></el-switch>
      </el-form-item>
      <el-form-item label="商品描述" prop="produceDesc">
        <!-- <el-input v-model="dataForm.produceDesc" placeholder="商品描述"></el-input> -->
        <textarea
          v-model="dataForm.produceDesc"
          style="margin: 5px; height: 226px; width: 96%"
          placeholder="商品描述"
        ></textarea>
      </el-form-item>
      <el-form-item label="商品封面图" prop="produceIcon">
        <!-- <el-input v-model="dataForm.produceIcon" placeholder="商品封面图"></el-input> -->
        <single-upload v-model="dataForm.produceIcon"></single-upload>
      </el-form-item>
      <el-form-item label="商品详情图" prop="produceImgs">
        <!-- <el-input v-model="dataForm.produceImgs" placeholder="商品详情图"></el-input> -->
        <single-upload v-model="dataForm.produceImgs"></single-upload>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import SingleUpload from "@/components/upload/singleUpload.vue";
export default {
  components: { SingleUpload },
  data() {
    return {
      visible: false,
      dataForm: {
        produceId: 0,
        storeId: 0,
        classifyId: "",
        categoryId: "",
        produceName: "",
        producePrice: "",
        produceDesc: "",
        produceIcon: "",
        produceStatus: 1,
        produceStock: "",
        produceImgs: "",
        classifies: [],
        categories: [],
      },
      dataRule: {
        // classifyId: [
        //   { required: true, message: "所属分类不能为空", trigger: "blur" },
        // ],
        // categoryId: [
        //   { required: true, message: "所属目录不能为空", trigger: "blur" },
        // ],
        produceName: [
          { required: true, message: "商品名称不能为空", trigger: "blur" },
        ],
        producePrice: [
          { required: true, message: "商品价格不能为空", trigger: "blur" },
        ],
        produceDesc: [
          { required: true, message: "商品描述不能为空", trigger: "blur" },
        ],
        produceIcon: [
          { required: true, message: "商品封面图不能为空", trigger: "blur" },
        ],
        produceStock: [
          { required: true, message: "商品库存不能为空", trigger: "blur" },
        ],
        produceImgs: [
          { required: true, message: "商品详情图不能为空", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    init(id, storeId) {
      this.dataForm.storeId = storeId;
      console.log("StoreId=", storeId);
      console.log("id=", id);
      this.dataForm.produceId = id || 0;
      this.visible = true;
      if (id <= 0) {
        this.$nextTick(() => {
          this.$refs["dataForm"].resetFields();
          this.$http
            .get(`${process.env.ADMIN_URL}/admin/classify/listClassifyNames`, {
              withCredentials: true,
            })
            .then(({ data }) => {
              this.dataForm.classifies = data;
            });
        });
        this.$nextTick(() => {
          this.$http
            .get(
              `${process.env.URL_HEAD}/store/category/getNames/${
                this.dataForm.storeId ? this.dataForm.storeId : storeId
              }`,
              {
                withCredentials: true,
              }
            )
            .then(({ data }) => {
              this.dataForm.categories = data;
            });
        });
      } else {
        this.$nextTick(() => {
          this.$refs["dataForm"].resetFields();
          if (this.dataForm.produceId) {
            this.$http
              .get(
                `${process.env.URL_HEAD}/produce/produce/info/${this.dataForm.produceId}`,
                {
                  withCredentials: true,
                }
              )
              .then(({ data }) => {
                if (data && data.code === 0) {
                  this.dataForm.storeId = data.produce.storeId;
                  this.dataForm.classifyId = data.produce.classifyId;
                  this.dataForm.categoryId = data.produce.categoryId;
                  this.dataForm.produceName = data.produce.produceName;
                  this.dataForm.producePrice = data.produce.producePrice;
                  this.dataForm.produceDesc = data.produce.produceDesc;
                  this.dataForm.produceIcon = data.produce.produceIcon;
                  this.dataForm.produceStatus = data.produce.produceStatus;
                  this.dataForm.produceStock = data.produce.produceStock;
                  this.dataForm.produceImgs = data.produce.produceImgs;
                  this.dataForm.classifies = data.classifies;
                  this.dataForm.categories = data.categories;
                }
              });
          }
        });
      }
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http
            .post(
              `${process.env.URL_HEAD}/produce/produce/${
                !this.dataForm.produceId ? "save" : "update"
              }`,
              {
                produceId: this.dataForm.produceId || undefined,
                storeId: this.dataForm.storeId,
                classifyId: this.dataForm.classifyId,
                categoryId: this.dataForm.categoryId,
                produceName: this.dataForm.produceName,
                producePrice: this.dataForm.producePrice,
                produceDesc: this.dataForm.produceDesc,
                produceIcon: this.dataForm.produceIcon,
                produceStatus: this.dataForm.produceStatus,
                produceStock: this.dataForm.produceStock,
                produceImgs: this.dataForm.produceImgs,
              },
              {
                withCredentials: true,
              }
            )
            .then(({ data }) => {
              if (data && data.code === 0) {
                this.$message({
                  message: "操作成功",
                  type: "success",
                  duration: 1500,
                  onClose: () => {
                    this.visible = false;
                    this.$emit("refreshDataList");
                  },
                });
              } else {
                this.$message.error(data.msg);
              }
            });
        }
      });
    },
  },
};
</script>
